package com.yuncheng.oaApi.controller;


import com.yuncheng.oaApi.vo.OaFlowVo;
import com.yuncheng.oaApi.vo.OaHysglVo;
import com.yuncheng.oaApi.vo.OaReturnVo;
import com.yuncheng.oaApi.vo.Result;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.oaApi.busService.OaDbrService;
import com.yuncheng.oaApi.busService.MyLcFlowService;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaLogConstant;
import com.yuncheng.oaApi.constant.OaHysglOaFlowConstants;
import com.yuncheng.oaApi.constant.OaLgqsOaFlowConstants;
import com.yuncheng.oaApi.entity.OaHysgl;
import com.yuncheng.oaApi.entity.OaJobEntity;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.lcService.OaHsyglOaFlowService;
import com.yuncheng.oaApi.mapper.OaHysglFlowMapper;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Api(tags = "OA-会议室管理申请接口")
@RestController
@RequestMapping("/api/spzx-oa/flow-hysgl")
public class OaHysglFlowController {

    private static final Logger log = LoggerFactory.getLogger(OaHysglFlowController.class);

    @Resource
    private OaHysglOaFlowConstants oaHysglFlowConstant;

    @Resource
    private OaHysglFlowMapper oaHysglFlowMapper;

    @Resource
    private OaHsyglOaFlowService oaHsyglFlowService;

    @Resource
    private IOaHysglService oaHysglService;

    @Resource
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private IOaSysTzMessageService sysTzMessageService;

    @Resource
    @Lazy
    private OaDbrService oaDbrService;

    @Resource
    @Lazy
    private MyLcFlowService myLcFlowService;


    /**
     * 流程列表分页查询
     *
     * @param oaHysglVo
     * @param pageNo
     * @param pageSize
     * @param onlyCurrentUser
     * @return
     */
    @AutoLog(value = "流程监控-分页查询")
    @ApiOperation(value = "流程监控-分页查询")
    @GetMapping("flowJkPage")
    public HttpResult<?> flowJkPage(
            OaHysglVo oaHysglVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "false") boolean onlyCurrentUser) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<OaHysglVo> page = new Page<>(pageNo, pageSize);
        oaHysglVo.setFlcmc(oaHysglFlowConstant.getFlowName());
        IPage<OaHysglVo> list = oaHysglFlowMapper.flowJkPage(page, onlyCurrentUser, user, oaHysglVo);
        return HttpResult.ok(list);
    }

    /**
     * 流程列表分页查询
     *
     * @param oaHysglVo
     * @param pageNo
     * @param pageSize
     * @param onlyCurrentUser
     * @return
     */
    @AutoLog(value = "流程列表-分页查询")
    @ApiOperation(value = "流程列表-分页查询")
    @GetMapping("flowJobPage")
    public HttpResult<?> flowJobPage(
            OaHysglVo oaHysglVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        if (StringUtils.isBlank(oaHysglVo.getFblzt())) {
            oaHysglVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        }
        Page<OaHysglVo> page = new Page<>(pageNo, pageSize);
        oaHysglVo.setFlcmc(oaHysglFlowConstant.getFlowName());
        IPage<OaHysglVo> list = oaHysglFlowMapper.flowJobPage(page, onlyCurrentUser, user, oaHysglVo);
        return HttpResult.ok(list);
    }


    /**
     * 我的已办
     */
    @AutoLog(value = "流程列表-我的已办")
    @ApiOperation(value = "流程列表-我的已办")
    @GetMapping("myJobPage")
    public HttpResult<?> myJobPage(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            OaHysglVo oaHysglVo) {

        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<OaHysglVo> page = new Page<>(pageNo, pageSize);
        IPage<OaHysglVo> list = oaHysglFlowMapper.myYbFlowPage(page, true, user, oaHysglVo);
        return HttpResult.ok(list);
    }

    /**
     * 数据修订
     */
    @AutoLog(value = "列表-数据修订")
    @ApiOperation(value = "列表-数据修订")
    @GetMapping("Pagesjxd")
    public HttpResult<?> Pagesjxd(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            OaHysglVo oaHysglVo) {

        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }

        Page<OaHysglVo> page = new Page<>(pageNo, pageSize);
        IPage<OaHysglVo> list = oaHysglFlowMapper.myYbFlowPage(page, false, user, oaHysglVo);
        return HttpResult.ok(list);
    }


    @AutoLog(value = "发起会议室申请流程")
    @ApiOperation(value = "发起会议室申请流程")
    @PostMapping(value = "initBzFlow")
    public HttpResult<?> initBzFlow(@RequestBody OaHysgl oaHysgl) {
        String oaid = "";
        //当前人的id
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (currentUser == null) {
            return HttpResult.error("当前登录已失效，请重新登录");
        }
        if (StringUtils.isNotBlank(oaHysgl.getId())) {

            OaHysgl oaids = oaHysglService.getById(oaHysgl.getId());
            ConvertUtils.register(new DateConvert(), java.util.Date.class);
            try {
                BeanUtils.populate(oaids, BeanUtils.describe(oaHysgl));
            } catch (IllegalAccessException e) {
                log.error("出错-保存事项【非法访问异常】,{}", e.getMessage());
            } catch (InvocationTargetException e) {
                log.error("出错-保存事项【调用TargetException】,{}", e.getMessage());
            } catch (NoSuchMethodException e) {
                log.error("出错-找不到可用方法", e.getMessage());
            }

            oaid = oaHysgl.getId();

            oaids.setFrq(oaHysgl.getFrq());
            oaHysglService.updateById(oaids);
        } else {
            oaid = GetUuIdUtils.ReplaceUuId();


            oaHysgl.setId(oaid);
            oaHysgl.setFuserid(currentUser.getId());
            oaHysgl.setFsyks(currentUser.getDeptName());
            oaHysgl.setFjbr(currentUser.getName());
            oaHysgl.setFrq(new Date());

            YcSysUser ycSysUserById = ycSysUserService.getYcSysUserById(currentUser.getId());
            oaHysgl.setFlxdh(ycSysUserById.getPhone());
            oaHysgl.setFlczt(OaCommonFlowConstant.FLOW_LCZB);
            oaHysglService.save(oaHysgl);
        }

        /**
         * @Author: lhb
         * @Description : 是否发起流程   如果是开始申请就跳转流程
         *               在这里设置 nextNodeName 指定下个环节节点 做当前跳转流程的判断，然后设置变量接收环节的值，写到nextNodeName中
         * @DateTime:
         * @Params: Fsffqlc
         */
        String msg = "保存成功";
        // 初始化流程
        Result result = oaHsyglFlowService.initJob(oaid, currentUser);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }else {
            OaJobEntity job = (OaJobEntity)result.getResult();
            if (job != null) {
                // 是否提交流程
                String nextnode = "";
                if (StringUtils.isNotBlank(oaHysgl.getFsffqlc()) && oaHysgl.getFsffqlc().equals("是")) {
                    String fhydd = oaHysgl.getFhydd();
                    String fhykssj = oaHysgl.getFhykssj();
                    String fhyjssj = oaHysgl.getFhyjssj();

//                    boolean aBoolean = oaHsyglFlowService.sfHydd(oaHysgl.getFhydd(), oaHysgl.getFhykssj(), oaHysgl.getFhyjssj());
//                    //做一次判断 判断当前申请的 会议室地点 和 会议开始时间 ，结束时间 是否有流程在走 如果有 就 提示 当前会议室有流程正在申请请选择其他时间段
//                    if (aBoolean) { //如果为true 表示占用
//                        return HttpResult.error("当前会议室[" + oaHysgl.getFhydd() + "]不支持当前[" + oaHysgl.getFhykssj() + "-" + oaHysgl.getFhyjssj() + "]时间段申请，当前会议室在此时间段内已有申请流程");
//
//                    }
                    //判断当前会议室是否已被申请完成
                    if (oaHsyglFlowService.sqhys(fhydd,fhykssj,fhyjssj)) {//当为true时表示时间有重叠
                        return HttpResult.error("当前会议室[" + oaHysgl.getFhydd() + "]不支持当前[" + oaHysgl.getFhykssj() + "-" + oaHysgl.getFhyjssj() + "]时间段申请，当前会议室已被申请");
                    }
                    //如果是科室负责人，就是科长 跳过 不能用 职务或者职称来判断 用科室负责人来判断 应不应该跳过科室审核环节
                    if (oaHysgl.getFsyks().contains(OaCommonFlowConstant.BGS_SP_FLOW)) {
                        nextnode = OaHysglOaFlowConstants.HJMC_BGSSP;
                    } else if (oaDbrService.brksfzr()) {
                        nextnode = OaHysglOaFlowConstants.HJMC_BGSSP;
                    } else {
                        nextnode = "";
                    }
                    Result result1 = oaHsyglFlowService.passJob(oaid, job.getId(), "同意", nextnode, "");
                    if (!result1.isSuccess()) {
                        return HttpResult.error(result.getMessage());
                    }
                    //提交完成后 更新待办信息
                    sysTzMessageService.tjMessage(oaid);
                    myLcFlowService.recall(oaid);
                    msg = "提交成功";
                }
            }
        }
        HttpResult http = new HttpResult(true);
        http.setMessage(msg);
        http.setResult(oaHysgl);
        return http;
    }

    /**
     * 会议室申请保存接口
     */
    @AutoLog(value = "会议室申请保存接口")
    @ApiOperation(value = "会议室申请保存接口")
    @PostMapping(value = "/saveFlow")
    public HttpResult<?> saveFlow(@RequestBody OaFlowVo oaFlowVo) {
        String submitType = OaCommonFlowConstant.SUBMIT_TYPE_SAVE; // 保存
        String jobId = oaFlowVo.getJobid(); // 当前流程任务id
        String oaid = oaFlowVo.getOaid(); // 当前办公主键id
        String opinion = oaFlowVo.getOpinion(); // 办理意见

        if (StringUtils.isBlank(jobId)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }
        // 流程处理
        Result httpResult = oaHsyglFlowService.handleFlow(oaFlowVo, null, jobId, oaid, submitType, opinion);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }
        Map map = new HashMap();
        HttpResult<Object> res = new HttpResult<>(true);
        res.setMessage("保存成功");
        res.setResult(map);
        return res;
    }

    /**
     * 会议室申请提交接口
     */
    @AutoLog(value = "会议室申请提交接口")
    @ApiOperation(value = "会议室申请提交接口")
    @PostMapping(value = "/submitFlow")
    public HttpResult<?> submitFlow(@RequestBody OaFlowVo oaFlowVo) {
        String submitType = OaCommonFlowConstant.SUBMIT_TYPE_SUBMIT; // 提交

        String jobId = oaFlowVo.getJobid();
        String oaid = oaFlowVo.getOaid();
        String opinion = oaFlowVo.getOpinion();
        OaHysgl oaId = oaHysglService.getById(oaid);
        OaJobEntity jobid = spcySqsxJobService.getById(jobId);

        if (jobid.getFhjmc().equals(OaHysglOaFlowConstants.HJMC_HYAP)) {

        } else {
            oaId.setFlczt(OaCommonFlowConstant.FLOW_LCZB);
        }
        if (StringUtils.isBlank(oaId.getFsffqlc())) {
            oaId.setFsffqlc("是");
        }

        oaHysglService.updateById(oaId);

        if (StringUtils.isBlank(jobId)) {
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }
        /* if (StringUtils.isBlank(opinion)){
            return HttpResult.error("请填写办理意见");
        }*/
        OaJobEntity nowJob = null;
        Result httpResult1 = oaHsyglFlowService.handleFlow(oaFlowVo, nowJob, jobId, oaid, submitType, opinion);
        if (!httpResult1.isSuccess()){
            return HttpResult.error(httpResult1.getMessage());
        }
        //提交完成后 更新待办信息
        sysTzMessageService.tjMessage(oaid);
        myLcFlowService.recall(oaid);
        return HttpResult.ok("提交成功");
    }


    /**
     * 查询当前流程任务详情-通过当前流程任务id
     */
    @AutoLog(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiImplicitParam(value = "jobid",name = "流程任务id",required = true)
    @GetMapping(value = "queryJobByJobId")
    public HttpResult<?> queryJobByJobId(@RequestParam("jobid") String jobid) {
        if (StringUtils.isBlank(jobid)) {
            return HttpResult.error("流程任务id不能为空");
        }
        Map map = oaHsyglFlowService.getJobConcernByJobId(jobid);
        return HttpResult.ok(map);
    }


    /**
     * 获取已办列表-通过当前流程id-自定义退回
     *
     * @param jobid
     * @return
     */
    @AutoLog(value = "获取已办列表-通过当前流程id-自定义退回")
    @ApiOperation(value = "获取已办列表-通过当前流程id-自定义退回")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobid", value = "流程任务id"),
            @ApiImplicitParam(name = "isZdhj", value = "是否指定环节，默认：true，指定")
    })
    @GetMapping(value = "queryAllYbJob")
    public HttpResult<?> queryAllYbJob(@RequestParam(value = "jobid", defaultValue = "") String jobid,
                                       @RequestParam(value = "isZdhj", defaultValue = "true") boolean isZdhj) {
        List<Map<String, String>> currentJobPrevious = oaHsyglFlowService.getCurrentJobPrevious(jobid, isZdhj);
        return HttpResult.ok(currentJobPrevious);
    }

    /**
     * 退回环节
     *
     * @return
     */
    @AutoLog(value = "自定义-退回环节")
    @ApiOperation(value = "自定义-退回环节")
    @PostMapping(value = "returnUpperzdyFlowJob")
    public HttpResult<?> returnUpperzdyFlowJob(@RequestBody OaReturnVo oaReturnVo) {
        oaHsyglFlowService.returnZdJob(oaReturnVo);
        return HttpResult.ok("退回成功");
    }

    /**
     * 通过当前流程id-退回第一步
     *
     * @param
     * @return
     */
    @AutoLog(value = "通过当前流程id-退回第一步")
    @ApiOperation(value = "通过当前流程id-退回第一步")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobid", value = "流程任务id"),
            @ApiImplicitParam(name = "oaid", value = "业务主表id"),
            @ApiImplicitParam(name = "opinion", value = "意见")
    })
    @GetMapping(value = "returnStart")
    public HttpResult<?> returnStart(String jobid, String oaId, String opinion) {
        Result result = oaHsyglFlowService.doJobs(jobid, opinion);
        if (!result.isSuccess()) {
            return HttpResult.error(result.getMessage());
        }
        oaHsyglFlowService.createSqJob(jobid, oaId, opinion);
        return HttpResult.ok("退回成功");
    }

    /**
     * 退回上一环节流程任务
     */
    @AutoLog(value = "退回上一环节流程任务")
    @ApiOperation(value = "退回上一环节流程任务")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobid", value = "流程任务id", required = true),
            @ApiImplicitParam(name = "opinion", value = "意见", required = true)
    })
    @PostMapping(value = "returnUpperFlowJob")
    public HttpResult<?> returnUpperFlowJob(@RequestBody Map map){
        String jobid = map.get("jobid").toString();
        String opinion = map.get("opinion").toString();
        Result result = oaHsyglFlowService.getReturnUpperFlowJob(jobid,opinion);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("退回成功");
    }


    @AutoLog(value = "删除会议室申请流程")
    @ApiOperation(value = "删除会议室申请流程")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobids",value = "流程任务id,逗号分割",required = true)
    })
    @DeleteMapping(value = "deleteHysglFlowJob")
    public HttpResult<?> deleteHysglFlowJob(@RequestParam("jobids")String jobids){
        if (StringUtils.isBlank(jobids)){
            return HttpResult.error("参数不能为空");
        }
        List<String> jobIdList = Arrays.asList(jobids.split(","));
        String logNr = "";
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(jobIdList)){
            List<OaJobEntity> jobList = spcySqsxJobService.listByIds(jobIdList);
            if (CollectionUtils.isNotEmpty(jobList)){
                for (OaJobEntity job : jobList) {
                    spcySqsxJobService.removeById(job.getId());
                    OaHysgl oaHysgl = oaHysglService.getById(job.getFpid());
                    if (oaHysgl != null) {
                        oaHysglService.removeById(job.getFpid());

                        logNr = "删除会议室申请" + job.getFhjmc() + ": ";
                        map = ObjectToMapUtils.objectToMap(oaHysgl);
                        logNr += LogUtils.MapToStringMethod(map);

                        spcyLogService.addLogs(job.getFhjmc(), logNr, job.getFpid(), OaLogConstant.LOGS_OA_HYSGL, OaCommonFlowConstant.LOGS_SYS_TYPE_SC);
                    }
                }
            }
        }

        return HttpResult.ok("删除成功");
    }


    @AutoLog(value = "查询会议室是否可用")
    @ApiOperation(value = "查询会议室是否可用")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "fhydd",value = "会议地点"),
            @ApiImplicitParam(name = "fhyksjs",value = "会议开始时间"),
            @ApiImplicitParam(name = "fhyjssj",value = "会议结束时间")
    })
    @PostMapping(value = "hysSfky")
        public HttpResult<?> hysSfky (@RequestBody OaHysgl oaHysgl){
        if (StringUtils.isBlank(oaHysgl.getFhydd())) {
            return HttpResult.error("会议室名称不能为空");
        }
        boolean aBoolean = oaHsyglFlowService.sfHydd(oaHysgl.getFhydd(), oaHysgl.getFhykssj(), oaHysgl.getFhyjssj());
        //做一次判断 判断当前申请的 会议室地点 和 会议开始时间 ，结束时间 是否有流程在走 如果有 就 提示 当前会议室有流程正在申请请选择其他时间段
        if (aBoolean) { //如果为true 表示占用
            return HttpResult.error("当前会议室[" + oaHysgl.getFhydd() + "]不支持当前[" + oaHysgl.getFhykssj() + "-" + oaHysgl.getFhyjssj() + "]时间段申请，当前会议室在此时间段内已有申请流程");

        }else { //为false 继续往下 判断
            boolean sqhys = oaHsyglFlowService.sqhys(oaHysgl.getFhydd(), oaHysgl.getFhykssj(), oaHysgl.getFhyjssj());
            if (sqhys){
                return HttpResult.error("当前会议室[" + oaHysgl.getFhydd() + "]不支持当前[" + oaHysgl.getFhykssj() + "-" + oaHysgl.getFhyjssj() + "]时间段申请，当前会议室已被申请");
            }else {
                return HttpResult.ok("当前会议室[" + oaHysgl.getFhydd() + "]支持当前[" + oaHysgl.getFhykssj() + "-" + oaHysgl.getFhyjssj() + "]时间段申请");
            }
        }
    }

    @AutoLog(value = "退回指定环节")
    @ApiOperation(value = "退回指定环节")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "jobid", name = "当前流程任务jobid", required = true),
            @ApiImplicitParam(value = "opinion", name = "退回意见", required = true),
            @ApiImplicitParam(value = "shjid", name = "选择退回的环节id", required = true)
    })
    @PostMapping(value = "returnUpperFlowJobs")
    public HttpResult<?> returnUpperFlowJobs(@RequestBody Map<String, String> map) {
        String jobid = map.get("jobid");
        String opinion = map.get("opinion");
        String shjid = map.get("shjid");

        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (currentUser == null) {
            return HttpResult.error("登录已失效，请重新登录");
        }
        OaJobEntity job = spcySqsxJobService.getById(jobid);
        // 待办-获取上一个环节id
        OaJobEntity syhjJob = spcySqsxJobService.getById(shjid);
        if (job == null) {
            return HttpResult.error("当前流程任务不存在");
        }
        if (syhjJob == null) {
            return HttpResult.error("当前选择退回的环节不存在");
        }
        // 办理状态 - 待办
        if (StringUtils.isNotBlank(job.getFblzt()) && (job.getFblzt().equals(OaCommonFlowConstant.BLZT_DB)
                || job.getFblzt().equals(OaCommonFlowConstant.BLZT_TH))) {
            String oldshjid = job.getFsyhjid(); // 当前流程上环节

            // 如果 当前 流程上环节与 选择 退回的环节 相等 则删除.否则不等，则修改办理状态
            if (!oldshjid.equals(shjid)) {
                String del = dealDelNodeJob(shjid);
            }
            if (syhjJob.getFhjmc().equals(OaLgqsOaFlowConstants.HJMC_QJSQ)) {
                OaHysgl sqsx = oaHysglService.getById(job.getFpid());
                if (sqsx != null) {
                    oaHysglService.updateById(sqsx);
                }
            }
            syhjJob.setFthyj(opinion);
            syhjJob.setFfssj(new Date());
            syhjJob.setFblzt(OaCommonFlowConstant.BLZT_TH);
            syhjJob.setFfsrid(currentUser.getId());
            syhjJob.setFfsrmc(currentUser.getName());
            spcySqsxJobService.updateById(syhjJob);

            spcySqsxJobService.removeById(jobid); // 删除当前流程任务待办
        }

        String logNr = "退回上一环节: 从[" + job.getFhjmc() + "]退回到[" + syhjJob.getFhjmc() + "];";
        Map<String, Object> logMap = ObjectToMapUtils.objectToMap(job);
        logNr += LogUtils.MapToStringMethod(logMap);
        spcyLogService.addLogs(job.getFhjmc(), logNr, job.getFpid(), OaLogConstant.LOGS_SQSX_JOB, OaCommonFlowConstant.LOGS_SYS_TYPE_TH);

        return HttpResult.ok("退回成功");
    }

    /**
     * 递归删除当前流程任务父级的所有流程任务（）
     */
    public String dealDelNodeJob(String shjid) {
        String id = "";
        List<OaJobEntity> shjList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getFsyhjid, shjid));
        if (CollectionUtils.isNotEmpty(shjList)) {
            for (OaJobEntity job : shjList) {
                id = job.getId();
                if (job.getFhjmc().equals(OaHysglOaFlowConstants.HJMC_SQ)) {
                    OaHysgl sqsx = oaHysglService.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaHysglService.updateById(sqsx);
                    }
                }

                if (job.getFhjmc().equals(OaHysglOaFlowConstants.HJMC_KSSP)) {
                    OaHysgl sqsx = oaHysglService.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaHysglService.updateById(sqsx);
                    }
                }

                if (job.getFhjmc().equals(OaHysglOaFlowConstants.HJMC_BGSSP)) {
                    OaHysgl sqsx = oaHysglService.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaHysglService.updateById(sqsx);
                    }
                }
                if (job.getFhjmc().equals(OaHysglOaFlowConstants.HJMC_HYAP)) {
                    OaHysgl sqsx = oaHysglService.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaHysglService.updateById(sqsx);
                    }
                }


                spcySqsxJobService.removeById(id);
                return dealDelNodeJob(id);
            }
        }
        return id;
    }

}
